/*
 *  Copyright (C) 2011 felixtheripper <felixtheripper_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.ctm.etk.gui.swg.display.frame;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import org.myorg.ctm.etk.gui.io.finder.FinderResEtk;
import org.myorg.shr.gui.swg.util.logging.FileHandlerShr;
import org.myorg.shr.gui.swg.lang.IShrObj;
import org.myorg.shr.gui.swg.display.frame.FrmAbs;
import org.myorg.shr.gui.swg.display.menuitem.MimAbs;

/**
 *
 * @author felixtheripper
 *
 * email: felixtheripper_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */

abstract public class FrmPrintableAbs extends FrmAbs implements
        Printable,
        IShrObj
{
    protected FrmPrintableAbs(String strTitle, int intW, int intH)
    {
        super(strTitle);

        setPreferredSize(new Dimension(intW, intH));

        ImageIcon iin = FinderResEtk.s_getIcon(FinderResEtk.STR_APPLI, MimAbs.ICN_SIZE);

        if (iin != null)
        {
            this.setIconImage(iin.getImage());
        }
        else
        {
            FrmPrintableAbs._LOGGER_.log(Level.WARNING, "nil iin, ImageIconFinder.STR_LOGOGFR16={0}", FinderResEtk.s_getIcon(FinderResEtk.STR_APPLI, MimAbs.ICN_SIZE).toString());
        }
    }

    public boolean doPrint()
    {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(this);

            boolean blnOk = true;

            if (job.printDialog())
            {
               try
               {
                       job.print();
               }

               catch(Exception exc)
               {
                       // TODO: redirect to log file
                       exc.printStackTrace();
                       blnOk = false;
               }
            }

            setCursor(Cursor.getDefaultCursor());
            return blnOk;
    }

    public int print(Graphics g, PageFormat pft, int intPageIndex)
    {
            Graphics2D g2 = (Graphics2D) g;

            g2.setColor(Color.black);
            Dimension d = this.getSize();

            double dblPanelW = d.getWidth();
            double dblPanelH = d.getHeight();

            double dblPageW = pft.getImageableWidth();
            double dblPageH = pft.getImageableHeight();

            double dblScale = dblPageW / dblPanelW;
            int intPageNb = (int) Math.ceil(dblScale * dblPanelH / dblPageH);

            if (intPageIndex >= intPageNb)
                    return Printable.NO_SUCH_PAGE;

            g2.translate(pft.getImageableX(), pft.getImageableY());

            g2.translate(0f, -intPageIndex*dblPageH);

            g2.scale(dblScale, dblScale);
            this.paint(g2);

            return Printable.PAGE_EXISTS;

    }

    static public JFrame s_getFrameOwner(Component cmp)
    {
        while (cmp != null)
        {
            if (cmp instanceof JFrame)
                return (JFrame) cmp;

            if (cmp instanceof JPopupMenu)
            {
                JPopupMenu pop = (JPopupMenu) cmp;
                JMenu men = (JMenu) pop.getInvoker();
                cmp = (Component) men;
            }
            cmp = cmp.getParent();
        }

        return null;
    }

    /*/
    static public JFrame s_getFrameOwner(Component cmp)
    {
        while (cmp != null)
        {
            if (cmp instanceof JFrame)
                return (JFrame) cmp;

            cmp = cmp.getParent();
        }

        return null;
    }
    //*/
    
        final static private Logger _LOGGER_ = Logger.getLogger(FrmPrintableAbs.class.getName());

    static  {
        FrmPrintableAbs._LOGGER_.addHandler(FileHandlerShr.s_getInstance());
    }
}
